package com.yuanfeng.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.activity.entity.ActivityShareGetRedpacketGradeEntity;
import com.yuanfeng.activity.entity.ActivityShareGetRedpacketRuleEntity;
import com.yuanfeng.activity.mapper.ActivityShareGetRedpacketGradeMapper;
import com.yuanfeng.activity.mapper.ActivityShareGetRedpacketRuleMapper;
import com.yuanfeng.activity.service.ActivityShareGetRedpacketRuleService;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 *
 */
@Service
public class ActivityShareGetRedpacketRuleServiceImpl extends ServiceImpl<ActivityShareGetRedpacketRuleMapper, ActivityShareGetRedpacketRuleEntity>
    implements ActivityShareGetRedpacketRuleService{

    @Autowired
    private ActivityShareGetRedpacketGradeMapper activityShareGetRedpacketGradeMapper;

    @Override
    public ResponseResult deleteShareGetRp(Map<String, Object> param) {
        this.baseMapper.deleteById(param.get("ruleId").toString());
        activityShareGetRedpacketGradeMapper.delete(new LambdaQueryWrapper<ActivityShareGetRedpacketGradeEntity>()
                                                .eq(ActivityShareGetRedpacketGradeEntity::getRuleId,Integer.valueOf(param.get("ruleId").toString())));

        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult queryShareGetRpList(Map<String, Object> param) {
        IPage<ActivityShareGetRedpacketRuleEntity> page=this.page(new Query<ActivityShareGetRedpacketRuleEntity>().getPage(param),
                new LambdaQueryWrapper<ActivityShareGetRedpacketRuleEntity>()
                .like(param.containsKey("ruleTitle"),ActivityShareGetRedpacketRuleEntity::getRuleTitle,param.get("ruleTitle").toString()));
        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult updateShareGetRp(Map<String, Object> param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.get("token").toString());
        if (null != userInfo) {
            String userRealname = userInfo.getUserRealname();
            if (null == userRealname) {
                userRealname = userInfo.getUserId();
            }
            param.put("gradeUpdateBy",userRealname);
        }
        param.put("updateDate",new Date());
        if (null == param.get("prizeId")) {
            this.baseMapper.insertShareGetRp(param);
        } else {
            this.baseMapper.updateShareGetRp(param);
        }

        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }
}




